﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Linq;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Metadata.Entity.DataModel;

namespace T.FAS.Runtime.DataAccess.Engine
{
    public class DataQueryHelper
    {
        public static TSchema BuildSchema(DataQuery metadata)
        {

            if (metadata == null)
                return null;
            return metadata.BuildSchema();
        }

        public static TResult BuildTResult(DataQuery metadata, object data, int count)
        {
            var schema = BuildSchema(metadata);
            TResult result = TResultBuilder.BuildTResult(data, schema);
            result.Count = count;
            return result;
        }

        public static DataType ConverDataType(string type)
        {
            switch (type)
            {
                case "binary":
                    return DataType.Binary;
                case "boolean":
                    return DataType.Boolean;
                case "int":
                    return DataType.Int;
                case "long":
                    return DataType.Long;
                case "decimal":
                    return DataType.Decimal;
                case "datetime":
                    return DataType.DateTime;
                case "timestamp":
                    return DataType.Timestamp;
                case "date":
                    return DataType.Date;
                case "time":
                    return DataType.Time;
                case "string":
                    return DataType.String;
                case "record":
                    return DataType.Record;
                case "list":
                    return DataType.List;
                default:
                    return DataType.String;
            }
        }

        public static object GetTypeDefaultValue(string type)
        {
            switch (type.ToLower())
            {
                case "binary":
                    return string.Empty;
                case "boolean": //bool值低代码运行时生成的是 bit(1) 0,1
                    return 0;
                case "int":
                    return 0;
                case "long":
                    return 0;
                case "decimal":
                    return 0;
                case "datetime":
                    //return default(DateTime);
                    return null;
                case "timestamp":
                    //return default(DateTime);
                    return null;
                case "date":
                    //return default(DateTime);
                    return null;
                case "time":
                    //return default(DateTime);
                    return null;
                case "string":
                    return string.Empty;
                default:
                    return default(object);
            }
        }

    }
}
